import axios from 'axios';
import { Paging } from '@/types/global';
import { FolderMessage, FileMessage } from '@/api/tree-folder';

export interface FileAndFolderMessage extends FolderMessage, FileMessage {
  /** 0:folder 1:file */
  isFileOrFolder?: number;
}

export interface FileVersion {
  createBy?: string;
  createDate?: string;
  deleteFlag?: number;
  description?: string;
  fileId?: string;
  fileToken?: string;
  folderId?: string;
  id?: string;
  isLocked?: boolean;
  name?: string;
  projectId?: string;
  size?: string;
  teamId?: string;
  type?: string;
  updateBy?: string;
  updateDate?: string;
  version?: string;
}

export function addChildFolder(folder?: FolderMessage) {
  return axios.post<string>('/cde-collaboration/folder/save', folder);
}

export function updateFolder(folder?: FolderMessage) {
  return axios.post<string>('/cde-collaboration/folder/update', folder);
}

export function updateFile(file?: FileMessage) {
  return axios.post<string>('/cde-collaboration/file/update', file);
}

export function addFile(file?: FileMessage) {
  return axios.post<string>('/cde-collaboration/file/save', file);
}

export function getFileVersionList(id?: string) {
  return axios.get<Paging<FileVersion>>(
    '/cde-collaboration/file/version/list',
    {
      params: {
        id,
        pageSize: 9999,
      },
    }
  );
}

export function fileRollback(id?: string, version?: string) {
  return axios.get<string>('/cde-collaboration/file/version/revert', {
    params: {
      id,
      version,
    },
  });
}

export function fileDownload(fileToekn: string) {
  return axios.get('/sys-storage/download', {
    params: {
      f8s: fileToekn,
    },
    responseType: 'blob',
    timeout: 0,
  });
}

export function fileShare(id: string) {
  return axios.get<string>('/sys-storage/download', {
    params: {
      id,
    },
  });
}
